#ifndef  __ZEN_MAP_GENERATOR_HPP__
#define  __ZEN_MAP_GENERATOR_HPP__

#include <string>
#include <stdlib.h>
#include <time.h>
/************************************************************************/
/* HeightMap Generator class:
this class allow the generation of pseudo random height maps.

the method generateHeightMap allow you to generate an height for each
point of the map, where the height is given by the following formula:
given two points p1, p2, the height of the central point is:

height = (height[p1] + height[p2]) / 2 +- distance * random(factor)

where factor is a fixed heightFactor used to have realistic height values
and the distance is y(p2) - y(p1)

*/
/************************************************************************/
namespace zen {
	namespace video {
		class MapGenerator {
		private:
			static void generateHeightMap(int y1, int y2, float* heightMapColumn, float heightFactor);
		public:
			static void generateHeightMap(float** heightMap, int dimension, int minHeight, int maxHeight, int heightFactor);
			static void createJSLFile(std::string path, std::string fileName, float const* const* heightMap);
		};

		void MapGenerator::generateHeightMap(int y1, int y2, float* heightMapColumn, float heightFactor) {
			if(y2 - y1 <= 1)
				return;
			int index = (y1 + y2) / 2;
			float randomValue = heightFactor * ((int) rand() % (y2-y1) - (y2-y1));
			heightMapColumn[index] =  (heightMapColumn[y1] + heightMapColumn[y2]) / 2 + randomValue;
			generateHeightMap(y1, index, heightMapColumn, heightFactor);
			generateHeightMap(index, y2, heightMapColumn, heightFactor);
		}

		void MapGenerator::generateHeightMap(float** heightMap, int dimension, int minHeight, int maxHeight, int heightFactor) {
			srand(time(NULL));
			int range = maxHeight-minHeight;
			for(int x = 0; x < dimension; x++) {
				heightMap[x][0] = rand() % (range);
				heightMap[x][dimension-1] = rand() % (range);
				generateHeightMap(0, dimension-1, heightMap[x], heightFactor);
			}
		}
	}// namespace video
}// namespace zen
#endif